<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Session;

/**
 *
 *财务管理
 * @icon fa fa-circle-o
 */
class Creditor extends Backend
{

    /**
     * Card模型对象
     * @var \app\admin\model\Card
     */
    protected $model = null;
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['ajaxIndex'];

    public function _initialize()
    {
        parent::_initialize();
    }

    //前端数据统计和明细
    public function ajaxIndex(){
        //总条件：当前登陆身份
        $adminId = Session::get("admin")["id"];
        $hid = $this->request->request('hid');
        if($hid == 1){
            $hid = Session::get("admin")["hotelid"];
        }

        $starttime = $this->request->request('starttime');
        $endtime = $this->request->request('endtime');

        $todayS=strtotime($starttime); //int
        $todayE= strtotime($endtime);//int

        $oderdb = db("order");
        $oadb = db("cash");//订单申请提现表
        $czdb = db("chongzhi");//充值表
        $ucdb = db("user_card");//小时卡
        $oderactdb = db("orderactivity");//套餐订单

        $res = [];


        if($todayS && !$todayE){
            $res['income']['shop_all_income'] = 0;
            $res['income']['shop_all_hour'] = 0;
            $res['list']['shop_all_ids'] = "";
            if($adminId == 1){

                //总收入 收入start
                //订单收入 单纯微信支付
                $order_all_income = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("pay_type = '0'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                // 小时卡支付中的微信支付款部分
                $order_all_wx_income = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time >= $todayS")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("wx_income");
                //退款产生的收益 也记录到现金 取消订单时 不论什么方式支付的，产生的收益记录到 income 而没有区分到 wx_income(退房记录到)
                $tk_income = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("pay_type != 1")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("income");

                //充值 （余额 包括未消费）
                $cz_all_income = $czdb->where("ctime >= $todayS")->sum("money");
                //小时卡 （包括未消费 只统计购买的，兑换的暂时平台没有记录入口，暂不统计）
                $uc_all_imcome = $ucdb->where(['type'=>'1'])->where("d_time >= '$starttime'")->sum("c_price");
                //总收入
                $all_income = sprintf("%1\$.2f", $order_all_income) + sprintf("%1\$.2f", $order_all_wx_income) + sprintf("%1\$.2f", $tk_income) + sprintf("%1\$.2f", $cz_all_income) + sprintf("%1\$.2f", $uc_all_imcome) ;
                $res['income']['all_income'] = sprintf("%1\$.2f", $all_income);
                //总收入 收入end

                //现金 记录和明细 start  现金包括 微信支付 小时卡支付中微信付款部分
                //订单统计（单纯微信支付 ids）
                $wxlist = $oderdb->where("hotelid in ($hid)")->where("pay_type = '0'")->where("add_time >= $todayS")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $wxids = "";
                if($wxlist){
                    foreach ($wxlist as $v){
                        $wxids .= $v['id'].",";
                    }
                }
                if($wxids != ""){
                    $wxids = rtrim($wxids,",");
                }
                $order_all_wx_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time >= $todayS")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $wxids2 = "";
                if($order_all_wx_income_ids){
                    foreach ($order_all_wx_income_ids as $v){
                        $wxids2 .= $v['id'].",";
                    }
                }
                if($wxids2 != ""){
                    $wxids2 = rtrim($wxids2,",");
                }

                //退款产生的收益 记录id
                $tk_income_ids = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("pay_type != 1")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $tkids = "";
                if($tk_income_ids){
                    foreach ($tk_income_ids as $v){
                        $tkids .= $v['id'].",";
                    }
                }
                if($tkids != ""){
                    $tkids = rtrim($tkids,",");
                }

                $allids = $wxids."||".$wxids2."||".$tkids;

                $xianjin = sprintf("%1\$.2f", $order_all_income);
                $xianjin2 = sprintf("%1\$.2f", $order_all_wx_income);
                $xianjin3 = sprintf("%1\$.2f", $tk_income);
                $res['income']['xianjin'] =  sprintf("%1\$.2f", $xianjin) +  sprintf("%1\$.2f", $xianjin2) +  sprintf("%1\$.2f", $xianjin3);
                $res['list']['wx_list']   = $allids;
                $res['list']['wx_sum']   = sprintf("%1\$.2f", $res['income']['xianjin']);
                //现金 记录和明细 end

                //充值记录 收入和明细 start
                //充值（包括未消费）
                $res['income']['cz_income'] = sprintf("%1\$.2f", $cz_all_income);
                //充值记录 chongzhi 表
                $cz_all_ids = $czdb->field("id")->select();
                $czallids = "";
                if($cz_all_ids){
                    foreach ($cz_all_ids as $v){
                        $czallids .= $v['id'].",";
                    }
                }
                if($czallids != ""){
                    $czallids = rtrim($czallids,",");
                }
                $res['list']['cz_list']   = $czallids;
                //充值记录 收入和明细 end

                //小时卡 收入和明细 start
                //小时卡 （包括未消费 只统计购买的，兑换的暂时平台没有记录入口，暂不统计）如果是商家这里则显示 小数数
                $res['income']['card_imcome'] = sprintf("%1\$.2f", $uc_all_imcome);
                $uc_all_ids = $ucdb->where(['type'=>'1'])->field("id")->select();
                $ucallids = "";
                if($uc_all_ids){
                    foreach ($uc_all_ids as $v){
                        $ucallids .= $v['id'].",";
                    }
                }
                if($ucallids != ""){
                    $ucallids = rtrim($ucallids,",");
                }
                $res['list']['xsk_list']   = $ucallids;
                $res['list']['xsk_sum']   = sprintf("%1\$.2f", $uc_all_imcome);
                //小时卡 收入和明细 end

                //退款 收入和明细 start
                //退款 统计退款的房间的费用 而不是收入 以及 套餐的退款 房间的退款：如果是小时房 退款统计单价 如果是全日房 统计封顶价
                $timing1 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '1'")->where("add_time >= $todayS")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing2 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '2'")->where("add_time >= $todayS")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing0 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '0'")->where("add_time >= $todayS")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $res['income']['tk_imcome'] = sprintf("%1\$.2f", $timing1) + sprintf("%1\$.2f", $timing2) + sprintf("%1\$.2f", $timing0);
                //订单统计（微信支付）
                $timing1_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '1'")->where("add_time >= $todayS")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing2_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '2'")->where("add_time >= $todayS")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing0_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '0'")->where("add_time >= $todayS")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $wxt1ids = "";
                if($timing1_ids){
                    foreach ($timing1_ids as $v){
                        $wxt1ids .= $v['id'].",";
                    }
                }
                if($wxt1ids != ""){
                    $wxt1ids = rtrim($wxt1ids,",");
                }
                $wxt2ids = "";
                if($timing2_ids){
                    foreach ($timing2_ids as $v){
                        $wxt2ids .= $v['id'].",";
                    }
                }
                if($wxt2ids != ""){
                    $wxt2ids = rtrim($wxt2ids,",");
                }
                $wxt0ids = "";
                if($timing0_ids){
                    foreach ($timing0_ids as $v){
                        $wxt0ids .= $v['id'].",";
                    }
                }
                if($wxt0ids != ""){
                    $wxt0ids = rtrim($wxt0ids,",");
                }
                $allckids = $wxt1ids."||".$wxt2ids."||".$wxt0ids;
                $res['list']['tk_list']   = $allckids;
                $res['list']['tk_sum']   = sprintf("%1\$.2f", $res['income']['tk_imcome']);
                //退款 收入和明细 end


                if($res){
                    $result = array("code"=>1,"rows" => $res);
                }else{
                    $result = array("code"=>0,"rows" => []);
                }

                return json($result);

            }else{
                //非超管 比如：商家管理员
                //订单收入  只统计 微信支付和余额支付 + 小时卡微信付款部分
                //微信支付+余额支付
                $order_all_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time >= $todayS")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                //小时卡（微信部分付款的收入）
                $order_all_wx_income = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time >= $todayS")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("wx_income");
                //退款产生的收益 也记录到现金 取消订单时 不论什么方式支付的，产生的收益记录到 income 而没有区分到 wx_income(退房记录到)
                $tk_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time >= $todayS")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("income");

                //商家显示小时数
                $order_all_wx_hour = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time >= $todayS")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("use_hour");
                //商家显示小时数 ids
                $order_hour_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time >= $todayS")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();

                $hourids = "";
                if($order_hour_ids){
                    foreach ($order_hour_ids as $v){
                        $hourids .= $v['id'].",";
                    }
                }
                if($hourids != ""){
                    $hourids = rtrim($hourids,",");
                }

                $res['list']['shop_all_ids'] = $hourids;

                //总收入
                $all_income = sprintf("%1\$.2f", $order_all_income) + sprintf("%1\$.2f", $order_all_wx_income) + sprintf("%1\$.2f", $tk_income);
                $res['income']['all_income'] = sprintf("%1\$.2f", $all_income);
                $res['income']['shop_all_hour'] = $order_all_wx_hour;
                $res['income']['shop_all_income'] = sprintf("%1\$.2f", $res['income']['all_income'] * 0.9);//普通商家 平台收取 10%费用

                //微信支付+余额支付 ids
                $wxlist = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("pay_type !=2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                //小时卡（微信部分付款的收入） ids
                $wxlist2 = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();

                $wxids = "";
                if($wxlist){
                    foreach ($wxlist as $v){
                        $wxids .= $v['id'].",";
                    }
                }
                if($wxids != ""){
                    $wxids = rtrim($wxids,",");
                }

                $wxids2 = "";
                if($wxlist2){
                    foreach ($wxlist2 as $v){
                        $wxids2 .= $v['id'].",";
                    }
                }
                if($wxids2 != ""){
                    $wxids2 = rtrim($wxids2,",");
                }

                //退款产生的收益 记录id
                $tk_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time >= $todayS")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $tkids = "";
                if($tk_income_ids){
                    foreach ($tk_income_ids as $v){
                        $tkids .= $v['id'].",";
                    }
                }
                if($tkids != ""){
                    $tkids = rtrim($tkids,",");
                }

                $allids = $wxids."||".$wxids2."||".$tkids;
                $res['list']['wx_list']   = $allids;

                //小时卡&微信 消费明细（订单）
                $xsklist = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("pay_type = '2'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $xsksum= $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("pay_type = '2'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                $xskids = "";
                if($xsklist){
                    foreach ($xsklist as $v){
                        $xskids .= $v['id'].",";
                    }
                }
                if($xskids != ""){
                    $xskids = rtrim($xskids,",");
                }
                $res['list']['xsk_list']   = $xskids;
                $res['list']['xsk_sum']   = sprintf("%1\$.2f", $xsksum);

                //退款 统计退款的房间的费用 而不是收入 以及 套餐的退款 房间的退款：如果是小时房 退款统计单价 如果是全日房 统计封顶价
                $timing1 = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing2 = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing0 = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $res['income']['tk_imcome'] = sprintf("%1\$.2f", $timing1) + sprintf("%1\$.2f", $timing2) + sprintf("%1\$.2f", $timing0);

                //订单统计（微信支付）
                $timing1_ids = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing2_ids = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing0_ids = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();

                $wxt1ids = "";
                if($timing1_ids){
                    foreach ($timing1_ids as $v){
                        $wxt1ids .= $v['id'].",";
                    }
                }
                if($wxt1ids != ""){
                    $wxt1ids = rtrim($wxt1ids,",");
                }

                $wxt2ids = "";
                if($timing2_ids){
                    foreach ($timing2_ids as $v){
                        $wxt2ids .= $v['id'].",";
                    }
                }
                if($wxt2ids != ""){
                    $wxt2ids = rtrim($wxt2ids,",");
                }

                $wxt0ids = "";
                if($timing0_ids){
                    foreach ($timing0_ids as $v){
                        $wxt0ids .= $v['id'].",";
                    }
                }
                if($wxt0ids != ""){
                    $wxt0ids = rtrim($wxt0ids,",");
                }

                $allckids = $wxt1ids."||".$wxt2ids."||".$wxt0ids;
                $res['list']['tk_list']   = $allckids;

                if($res){
                    $result = array("code"=>1,"rows" => $res);
                }else{
                    $result = array("code"=>0,"rows" => []);
                }

                return json($result);
            }


        }
        if(!$todayS && $todayE){
            $res['income']['shop_all_income'] = 0;
            $res['income']['shop_all_hour'] = 0;
            $res['list']['shop_all_ids'] = "";
            if($adminId == 1){

                //总收入 收入start
                //订单收入 单纯微信支付
                $order_all_income = $oderdb->where("hotelid in ($hid)")->where("add_time <= '$todayE'")->where("pay_type = '0'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                // 小时卡支付中的微信支付款部分
                $order_all_wx_income = $oderdb->where("hotelid in ($hid)")->where("add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("wx_income");
                //退款产生的收益 也记录到现金 取消订单时 不论什么方式支付的，产生的收益记录到 income 而没有区分到 wx_income(退房记录到)
                $tk_income = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_type != 1")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("income");



                //充值 （余额 包括未消费）
                $cz_all_income = $czdb->where("ctime <= $todayE")->sum("money");
                //小时卡 （包括未消费 只统计购买的，兑换的暂时平台没有记录入口，暂不统计）
                $uc_all_imcome = $ucdb->where(['type'=>'1'])->where("d_time <= '$endtime'")->sum("c_price");
                //总收入
                $all_income = sprintf("%1\$.2f", $order_all_income) + sprintf("%1\$.2f", $order_all_wx_income) + sprintf("%1\$.2f", $tk_income) + sprintf("%1\$.2f", $cz_all_income) + sprintf("%1\$.2f", $uc_all_imcome) ;
                $res['income']['all_income'] = sprintf("%1\$.2f", $all_income);
                //总收入 收入end

                //现金 记录和明细 start  现金包括 微信支付 小时卡支付中微信付款部分
                //订单统计（单纯微信支付 ids）
                $wxlist = $oderdb->where("hotelid in ($hid)")->where("pay_type = '0'")->where("add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $wxids = "";
                if($wxlist){
                    foreach ($wxlist as $v){
                        $wxids .= $v['id'].",";
                    }
                }
                if($wxids != ""){
                    $wxids = rtrim($wxids,",");
                }
                $order_all_wx_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $wxids2 = "";
                if($order_all_wx_income_ids){
                    foreach ($order_all_wx_income_ids as $v){
                        $wxids2 .= $v['id'].",";
                    }
                }
                if($wxids2 != ""){
                    $wxids2 = rtrim($wxids2,",");
                }

                //退款产生的收益 记录id
                $tk_income_ids = $oderdb->where("hotelid in ($hid)")->where("add_time <= '$todayE'")->where("pay_type != 1")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $tkids = "";
                if($tk_income_ids){
                    foreach ($tk_income_ids as $v){
                        $tkids .= $v['id'].",";
                    }
                }
                if($tkids != ""){
                    $tkids = rtrim($tkids,",");
                }

                $allids = $wxids."||".$wxids2."||".$tkids;

                $xianjin = sprintf("%1\$.2f", $order_all_income);
                $xianjin2 = sprintf("%1\$.2f", $order_all_wx_income);
                $xianjin3 = sprintf("%1\$.2f", $tk_income);
                $res['income']['xianjin'] =  sprintf("%1\$.2f", $xianjin) +  sprintf("%1\$.2f", $xianjin2) +  sprintf("%1\$.2f", $xianjin3);
                $res['list']['wx_list']   = $allids;
                $res['list']['wx_sum']   = sprintf("%1\$.2f", $res['income']['xianjin']);
                //现金 记录和明细 end

                //充值记录 收入和明细 start
                //充值（包括未消费）
                $res['income']['cz_income'] = sprintf("%1\$.2f", $cz_all_income);
                //充值记录 chongzhi 表
                $cz_all_ids = $czdb->field("id")->select();
                $czallids = "";
                if($cz_all_ids){
                    foreach ($cz_all_ids as $v){
                        $czallids .= $v['id'].",";
                    }
                }
                if($czallids != ""){
                    $czallids = rtrim($czallids,",");
                }
                $res['list']['cz_list']   = $czallids;
                //充值记录 收入和明细 end

                //小时卡 收入和明细 start
                //小时卡 （包括未消费 只统计购买的，兑换的暂时平台没有记录入口，暂不统计）如果是商家这里则显示 小数数
                $res['income']['card_imcome'] = sprintf("%1\$.2f", $uc_all_imcome);
                $uc_all_ids = $ucdb->where(['type'=>'1'])->field("id")->select();
                $ucallids = "";
                if($uc_all_ids){
                    foreach ($uc_all_ids as $v){
                        $ucallids .= $v['id'].",";
                    }
                }
                if($ucallids != ""){
                    $ucallids = rtrim($ucallids,",");
                }
                $res['list']['xsk_list']   = $ucallids;
                $res['list']['xsk_sum']   = sprintf("%1\$.2f", $uc_all_imcome);
                //小时卡 收入和明细 end

                //退款 收入和明细 start
                //退款 统计退款的房间的费用 而不是收入 以及 套餐的退款 房间的退款：如果是小时房 退款统计单价 如果是全日房 统计封顶价
                $timing1 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '1'")->where("add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing2 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '2'")->where("add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing0 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '0'")->where("add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $res['income']['tk_imcome'] = sprintf("%1\$.2f", $timing1) + sprintf("%1\$.2f", $timing2) + sprintf("%1\$.2f", $timing0);
                //订单统计（微信支付）
                $timing1_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '1'")->where("add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing2_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '2'")->where("add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing0_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '0'")->where("add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $wxt1ids = "";
                if($timing1_ids){
                    foreach ($timing1_ids as $v){
                        $wxt1ids .= $v['id'].",";
                    }
                }
                if($wxt1ids != ""){
                    $wxt1ids = rtrim($wxt1ids,",");
                }
                $wxt2ids = "";
                if($timing2_ids){
                    foreach ($timing2_ids as $v){
                        $wxt2ids .= $v['id'].",";
                    }
                }
                if($wxt2ids != ""){
                    $wxt2ids = rtrim($wxt2ids,",");
                }
                $wxt0ids = "";
                if($timing0_ids){
                    foreach ($timing0_ids as $v){
                        $wxt0ids .= $v['id'].",";
                    }
                }
                if($wxt0ids != ""){
                    $wxt0ids = rtrim($wxt0ids,",");
                }
                $allckids = $wxt1ids."||".$wxt2ids."||".$wxt0ids;
                $res['list']['tk_list']   = $allckids;
                $res['list']['tk_sum']   = sprintf("%1\$.2f", $res['income']['tk_imcome']);
                //退款 收入和明细 end


                if($res){
                    $result = array("code"=>1,"rows" => $res);
                }else{
                    $result = array("code"=>0,"rows" => []);
                }

                return json($result);

            }else{

                //非超管 比如：商家管理员
                //订单收入  只统计 微信支付和余额支付 + 小时卡微信付款部分
                //微信支付+余额支付
                $order_all_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time <= $todayE")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                //小时卡（微信部分付款的收入）
                $order_all_wx_income = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time <= $todayE")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("wx_income");
                //退款产生的收益 也记录到现金 取消订单时 不论什么方式支付的，产生的收益记录到 income 而没有区分到 wx_income(退房记录到)
                $tk_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time <= $todayE")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("income");



                //商家显示小时数
                $order_all_wx_hour = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time <= $todayE")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("use_hour");
                //商家显示小时数 ids
                $order_hour_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time <= $todayE")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();




                $hourids = "";
                if($order_hour_ids){
                    foreach ($order_hour_ids as $v){
                        $hourids .= $v['id'].",";
                    }
                }
                if($hourids != ""){
                    $hourids = rtrim($hourids,",");
                }

                $res['list']['shop_all_ids'] = $hourids;

                //总收入
                $all_income = sprintf("%1\$.2f", $order_all_income) + sprintf("%1\$.2f", $order_all_wx_income) + sprintf("%1\$.2f", $tk_income);
                $res['income']['all_income'] = sprintf("%1\$.2f", $all_income);
                $res['income']['shop_all_hour'] = $order_all_wx_hour;
                $res['income']['shop_all_income'] = sprintf("%1\$.2f", $res['income']['all_income'] * 0.9);//普通商家 平台收取 10%费用

                //微信支付+余额支付 ids
                $wxlist = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("pay_type !=2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                //小时卡（微信部分付款的收入） ids
                $wxlist2 = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();

                $wxids = "";
                if($wxlist){
                    foreach ($wxlist as $v){
                        $wxids .= $v['id'].",";
                    }
                }
                if($wxids != ""){
                    $wxids = rtrim($wxids,",");
                }

                $wxids2 = "";
                if($wxlist2){
                    foreach ($wxlist2 as $v){
                        $wxids2 .= $v['id'].",";
                    }
                }
                if($wxids2 != ""){
                    $wxids2 = rtrim($wxids2,",");
                }
                //退款产生的收益 记录id
                $tk_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time <= $todayE")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $tkids = "";
                if($tk_income_ids){
                    foreach ($tk_income_ids as $v){
                        $tkids .= $v['id'].",";
                    }
                }
                if($tkids != ""){
                    $tkids = rtrim($tkids,",");
                }

                $allids = $wxids."||".$wxids2."||".$tkids;

                $res['list']['wx_list']   = $allids;


                //小时卡&微信 消费明细（订单）
                $xsklist = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("pay_type = '2'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $xsksum= $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("pay_type = '2'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                $xskids = "";
                if($xsklist){
                    foreach ($xsklist as $v){
                        $xskids .= $v['id'].",";
                    }
                }
                if($xskids != ""){
                    $xskids = rtrim($xskids,",");
                }
                $res['list']['xsk_list']   = $xskids;
                $res['list']['xsk_sum']   = sprintf("%1\$.2f", $xsksum);

                //退款 统计退款的房间的费用 而不是收入 以及 套餐的退款 房间的退款：如果是小时房 退款统计单价 如果是全日房 统计封顶价
                $timing1 = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing2 = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing0 = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $res['income']['tk_imcome'] = sprintf("%1\$.2f", $timing1) + sprintf("%1\$.2f", $timing2) + sprintf("%1\$.2f", $timing0);

                //订单统计（微信支付）
                $timing1_ids = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing2_ids = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing0_ids = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();

                $wxt1ids = "";
                if($timing1_ids){
                    foreach ($timing1_ids as $v){
                        $wxt1ids .= $v['id'].",";
                    }
                }
                if($wxt1ids != ""){
                    $wxt1ids = rtrim($wxt1ids,",");
                }

                $wxt2ids = "";
                if($timing2_ids){
                    foreach ($timing2_ids as $v){
                        $wxt2ids .= $v['id'].",";
                    }
                }
                if($wxt2ids != ""){
                    $wxt2ids = rtrim($wxt2ids,",");
                }

                $wxt0ids = "";
                if($timing0_ids){
                    foreach ($timing0_ids as $v){
                        $wxt0ids .= $v['id'].",";
                    }
                }
                if($wxt0ids != ""){
                    $wxt0ids = rtrim($wxt0ids,",");
                }

                $allckids = $wxt1ids."||".$wxt2ids."||".$wxt0ids;
                $res['list']['tk_list']   = $allckids;

                if($res){
                    $result = array("code"=>1,"rows" => $res);
                }else{
                    $result = array("code"=>0,"rows" => []);
                }

                return json($result);
            }



        }
        if($todayS && $todayE){
            $res['income']['shop_all_income'] = 0;
            $res['income']['shop_all_hour'] = 0;
            $res['list']['shop_all_ids'] = "";
            if($adminId == 1){

                //总收入 收入start
                //订单收入 单纯微信支付
                $order_all_income = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_type = '0'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                // 小时卡支付中的微信支付款部分
                $order_all_wx_income = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("wx_income");
                //退款产生的收益 也记录到现金 取消订单时 不论什么方式支付的，产生的收益记录到 income 而没有区分到 wx_income(退房记录到)
                $tk_income = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_type != 1")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("income");


                //充值 （余额 包括未消费）
                $cz_all_income = $czdb->where("ctime >= $todayS and ctime <= $todayE")->sum("money");
                //小时卡 （包括未消费 只统计购买的，兑换的暂时平台没有记录入口，暂不统计）
                $uc_all_imcome = $ucdb->where(['type'=>'1'])->where("d_time >= '$starttime' and d_time <= '$endtime'")->sum("c_price");
                //总收入
                $all_income = sprintf("%1\$.2f", $order_all_income) + sprintf("%1\$.2f", $order_all_wx_income) + sprintf("%1\$.2f", $tk_income) + sprintf("%1\$.2f", $cz_all_income) + sprintf("%1\$.2f", $uc_all_imcome) ;
                $res['income']['all_income'] = sprintf("%1\$.2f", $all_income);
                //总收入 收入end

                //现金 记录和明细 start  现金包括 微信支付 小时卡支付中微信付款部分
                //订单统计（单纯微信支付 ids）
                $wxlist = $oderdb->where("hotelid in ($hid)")->where("pay_type = '0'")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $wxids = "";
                if($wxlist){
                    foreach ($wxlist as $v){
                        $wxids .= $v['id'].",";
                    }
                }
                if($wxids != ""){
                    $wxids = rtrim($wxids,",");
                }
                $order_all_wx_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $wxids2 = "";
                if($order_all_wx_income_ids){
                    foreach ($order_all_wx_income_ids as $v){
                        $wxids2 .= $v['id'].",";
                    }
                }
                if($wxids2 != ""){
                    $wxids2 = rtrim($wxids2,",");
                }

                //退款产生的收益 记录id
                $tk_income_ids = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_type != 1")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $tkids = "";
                if($tk_income_ids){
                    foreach ($tk_income_ids as $v){
                        $tkids .= $v['id'].",";
                    }
                }
                if($tkids != ""){
                    $tkids = rtrim($tkids,",");
                }

                $allids = $wxids."||".$wxids2."||".$tkids;

                $xianjin = sprintf("%1\$.2f", $order_all_income);
                $xianjin2 = sprintf("%1\$.2f", $order_all_wx_income);
                $xianjin3 = sprintf("%1\$.2f", $tk_income);
                $res['income']['xianjin'] =  sprintf("%1\$.2f", $xianjin) + sprintf("%1\$.2f", $xianjin2) + sprintf("%1\$.2f", $xianjin3);
                $res['list']['wx_list']   = $allids;
                $res['list']['wx_sum']   = sprintf("%1\$.2f", $res['income']['xianjin']);
                //现金 记录和明细 end

                //充值记录 收入和明细 start
                //充值（包括未消费）
                $res['income']['cz_income'] = sprintf("%1\$.2f", $cz_all_income);
                //充值记录 chongzhi 表
                $cz_all_ids = $czdb->field("id")->select();
                $czallids = "";
                if($cz_all_ids){
                    foreach ($cz_all_ids as $v){
                        $czallids .= $v['id'].",";
                    }
                }
                if($czallids != ""){
                    $czallids = rtrim($czallids,",");
                }
                $res['list']['cz_list']   = $czallids;
                //充值记录 收入和明细 end

                //小时卡 收入和明细 start
                //小时卡 （包括未消费 只统计购买的，兑换的暂时平台没有记录入口，暂不统计）如果是商家这里则显示 小数数
                $res['income']['card_imcome'] = sprintf("%1\$.2f", $uc_all_imcome);
                $uc_all_ids = $ucdb->where(['type'=>'1'])->field("id")->select();
                $ucallids = "";
                if($uc_all_ids){
                    foreach ($uc_all_ids as $v){
                        $ucallids .= $v['id'].",";
                    }
                }
                if($ucallids != ""){
                    $ucallids = rtrim($ucallids,",");
                }
                $res['list']['xsk_list']   = $ucallids;
                $res['list']['xsk_sum']   = sprintf("%1\$.2f", $uc_all_imcome);
                //小时卡 收入和明细 end

                //退款 收入和明细 start
                //退款 统计退款的房间的费用 而不是收入 以及 套餐的退款 房间的退款：如果是小时房 退款统计单价 如果是全日房 统计封顶价
                $timing1 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '1'")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing2 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '2'")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing0 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '0'")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $res['income']['tk_imcome'] = sprintf("%1\$.2f", $timing1) + sprintf("%1\$.2f", $timing2) + sprintf("%1\$.2f", $timing0);
                //订单统计（微信支付）
                $timing1_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '1'")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing2_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '2'")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing0_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '0'")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $wxt1ids = "";
                if($timing1_ids){
                    foreach ($timing1_ids as $v){
                        $wxt1ids .= $v['id'].",";
                    }
                }
                if($wxt1ids != ""){
                    $wxt1ids = rtrim($wxt1ids,",");
                }
                $wxt2ids = "";
                if($timing2_ids){
                    foreach ($timing2_ids as $v){
                        $wxt2ids .= $v['id'].",";
                    }
                }
                if($wxt2ids != ""){
                    $wxt2ids = rtrim($wxt2ids,",");
                }
                $wxt0ids = "";
                if($timing0_ids){
                    foreach ($timing0_ids as $v){
                        $wxt0ids .= $v['id'].",";
                    }
                }
                if($wxt0ids != ""){
                    $wxt0ids = rtrim($wxt0ids,",");
                }
                $allckids = $wxt1ids."||".$wxt2ids."||".$wxt0ids;
                $res['list']['tk_list']   = $allckids;
                $res['list']['tk_sum']   = sprintf("%1\$.2f", $res['income']['tk_imcome']);
                //退款 收入和明细 end


                if($res){
                    $result = array("code"=>1,"rows" => $res);
                }else{
                    $result = array("code"=>0,"rows" => []);
                }

                return json($result);



            }else{

                //非超管 比如：商家管理员
                //订单收入  只统计 微信支付和余额支付 + 小时卡微信付款部分
                //微信支付+余额支付
                $order_all_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                //小时卡（微信部分付款的收入）
                $order_all_wx_income = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("wx_income");
                //退款产生的收益 也记录到现金 取消订单时 不论什么方式支付的，产生的收益记录到 income 而没有区分到 wx_income(退房记录到)
                $tk_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("income");

                //商家显示小时数
                $order_all_wx_hour = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("use_hour");
                //商家显示小时数 ids
                $order_hour_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();

                $hourids = "";
                if($order_hour_ids){
                    foreach ($order_hour_ids as $v){
                        $hourids .= $v['id'].",";
                    }
                }
                if($hourids != ""){
                    $hourids = rtrim($hourids,",");
                }

                $res['list']['shop_all_ids'] = $hourids;

                //总收入
                $all_income = sprintf("%1\$.2f", $order_all_income) + sprintf("%1\$.2f", $order_all_wx_income) + sprintf("%1\$.2f", $tk_income);
                $res['income']['all_income'] = sprintf("%1\$.2f", $all_income);
                $res['income']['shop_all_hour'] = $order_all_wx_hour;
                $res['income']['shop_all_income'] = sprintf("%1\$.2f", $res['income']['all_income'] * 0.9);//普通商家 平台收取 10%费用

                //微信支付+余额支付 ids
                $wxlist = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_type !=2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                //小时卡（微信部分付款的收入） ids
                $wxlist2 = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();

                $wxids = "";
                if($wxlist){
                    foreach ($wxlist as $v){
                        $wxids .= $v['id'].",";
                    }
                }
                if($wxids != ""){
                    $wxids = rtrim($wxids,",");
                }

                $wxids2 = "";
                if($wxlist2){
                    foreach ($wxlist2 as $v){
                        $wxids2 .= $v['id'].",";
                    }
                }
                if($wxids2 != ""){
                    $wxids2 = rtrim($wxids2,",");
                }
                //退款产生的收益 记录id
                $tk_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $tkids = "";
                if($tk_income_ids){
                    foreach ($tk_income_ids as $v){
                        $tkids .= $v['id'].",";
                    }
                }
                if($tkids != ""){
                    $tkids = rtrim($tkids,",");
                }

                $allids = $wxids."||".$wxids2."||".$tkids;
                $res['list']['wx_list']   = $allids;


                //小时卡&微信 消费明细（订单）
                $xsklist = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_type = '2'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $xsksum= $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("pay_type = '2'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                $xskids = "";
                if($xsklist){
                    foreach ($xsklist as $v){
                        $xskids .= $v['id'].",";
                    }
                }
                if($xskids != ""){
                    $xskids = rtrim($xskids,",");
                }
                $res['list']['xsk_list']   = $xskids;
                $res['list']['xsk_sum']   = sprintf("%1\$.2f", $xsksum);

                //退款 统计退款的房间的费用 而不是收入 以及 套餐的退款 房间的退款：如果是小时房 退款统计单价 如果是全日房 统计封顶价
                $timing1 = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing2 = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing0 = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $res['income']['tk_imcome'] = sprintf("%1\$.2f", $timing1) + sprintf("%1\$.2f", $timing2) + sprintf("%1\$.2f", $timing0);

                //订单统计（微信支付）
                $timing1_ids = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing2_ids = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing0_ids = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();

                $wxt1ids = "";
                if($timing1_ids){
                    foreach ($timing1_ids as $v){
                        $wxt1ids .= $v['id'].",";
                    }
                }
                if($wxt1ids != ""){
                    $wxt1ids = rtrim($wxt1ids,",");
                }

                $wxt2ids = "";
                if($timing2_ids){
                    foreach ($timing2_ids as $v){
                        $wxt2ids .= $v['id'].",";
                    }
                }
                if($wxt2ids != ""){
                    $wxt2ids = rtrim($wxt2ids,",");
                }

                $wxt0ids = "";
                if($timing0_ids){
                    foreach ($timing0_ids as $v){
                        $wxt0ids .= $v['id'].",";
                    }
                }
                if($wxt0ids != ""){
                    $wxt0ids = rtrim($wxt0ids,",");
                }

                $allckids = $wxt1ids."||".$wxt2ids."||".$wxt0ids;
                $res['list']['tk_list']   = $allckids;

                if($res){
                    $result = array("code"=>1,"rows" => $res);
                }else{
                    $result = array("code"=>0,"rows" => []);
                }

                return json($result);
            }



        }
        if(!$todayS && !$todayE){

            $res['income']['shop_all_income'] = 0;
            $res['income']['shop_all_hour'] = 0;
            $res['list']['shop_all_ids'] = "";
            //paystatus : 0 未支付 1 已预定（支付） 2 已入住（支付,待退房状态）3 已完成（支付后，退房，房间回归打扫）  4 已取消（未付款，未住，30分钟后，自动取消房间预定，回归空闲，或者后台管理员手动取消，前台用户手动取消） 5支付失败（房间回归空闲）
            //refund_status : 0 默认 1 抵消房费(无需退款）2 申请退款 3 已退款 4退款失败
            //tk_status 0未退款 1已退款  这点可以不用考虑 因为收入是根据完成状态的时候计算的，应该要退的款不在统计内 ：   思路分析
            //第一梯度 总收入 现金（微信） 充值 小时卡 退款

            //总收入：订单的收入 充值（包括未消费）小时卡（包括未消费） 以及 套餐的订单
            //超级管理员
            if($adminId == 1){
                //总收入 收入start
                //订单收入 单纯微信支付
                $order_all_income = $oderdb->where("hotelid in ($hid)")->where("pay_type = '0'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                // 小时卡支付中的微信支付款部分
                $order_all_wx_income = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("wx_income");
                //退款产生的收益 也记录到现金 取消订单时 不论什么方式支付的，产生的收益记录到 income 而没有区分到 wx_income(退房记录到)
                $tk_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("income");

                //充值 （余额 包括未消费）
                $cz_all_income = $czdb->sum("money");
                //小时卡 （包括未消费 只统计购买的，兑换的暂时平台没有记录入口，暂不统计）
                $uc_all_imcome = $ucdb->where(['type'=>'1'])->sum("c_price");
                //总收入
                $all_income = sprintf("%1\$.2f", $order_all_income) + sprintf("%1\$.2f", $order_all_wx_income) + sprintf("%1\$.2f", $tk_income) + sprintf("%1\$.2f", $cz_all_income) + sprintf("%1\$.2f", $uc_all_imcome) ;
                $res['income']['all_income'] = sprintf("%1\$.2f", $all_income);
                //总收入 收入end

                //现金 记录和明细 start  现金包括 微信支付 小时卡支付中微信付款部分
                //订单统计（单纯微信支付 ids）
                $wxlist = $oderdb->where("hotelid in ($hid)")->where("pay_type = '0'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $wxids = "";
                if($wxlist){
                    foreach ($wxlist as $v){
                        $wxids .= $v['id'].",";
                    }
                }
                if($wxids != ""){
                    $wxids = rtrim($wxids,",");
                }
                $order_all_wx_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $wxids2 = "";
                if($order_all_wx_income_ids){
                    foreach ($order_all_wx_income_ids as $v){
                        $wxids2 .= $v['id'].",";
                    }
                }
                if($wxids2 != ""){
                    $wxids2 = rtrim($wxids2,",");
                }
                //退款产生的收益 记录id
                $tk_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $tkids = "";
                if($tk_income_ids){
                    foreach ($tk_income_ids as $v){
                        $tkids .= $v['id'].",";
                    }
                }
                if($tkids != ""){
                    $tkids = rtrim($tkids,",");
                }

                $allids = $wxids."||".$wxids2."||".$tkids;
                $xianjin = sprintf("%1\$.2f", $order_all_income);
                $xianjin2 = sprintf("%1\$.2f", $order_all_wx_income);
                $xianjin3 = sprintf("%1\$.2f", $tk_income);
                $res['income']['xianjin'] =  sprintf("%1\$.2f", $xianjin) +  sprintf("%1\$.2f", $xianjin2) + sprintf("%1\$.2f", $xianjin3);;
                $res['list']['wx_list']   = $allids;
                $res['list']['wx_sum']   = sprintf("%1\$.2f", $res['income']['xianjin']);
                //现金 记录和明细 end

                //充值记录 收入和明细 start
                //充值（包括未消费）
                $res['income']['cz_income'] = sprintf("%1\$.2f", $cz_all_income);
                //充值记录 chongzhi 表
                $cz_all_ids = $czdb->field("id")->select();
                $czallids = "";
                if($cz_all_ids){
                    foreach ($cz_all_ids as $v){
                        $czallids .= $v['id'].",";
                    }
                }
                if($czallids != ""){
                    $czallids = rtrim($czallids,",");
                }
                $res['list']['cz_list']   = $czallids;
                //充值记录 收入和明细 end

                //小时卡 收入和明细 start
                //小时卡 （包括未消费 只统计购买的，兑换的暂时平台没有记录入口，暂不统计）如果是商家这里则显示 小数数
                $res['income']['card_imcome'] = sprintf("%1\$.2f", $uc_all_imcome);
                $uc_all_ids = $ucdb->where(['type'=>'1'])->field("id")->select();
                $ucallids = "";
                if($uc_all_ids){
                    foreach ($uc_all_ids as $v){
                        $ucallids .= $v['id'].",";
                    }
                }
                if($ucallids != ""){
                    $ucallids = rtrim($ucallids,",");
                }
                $res['list']['xsk_list']   = $ucallids;
                $res['list']['xsk_sum']   = sprintf("%1\$.2f", $uc_all_imcome);
                //小时卡 收入和明细 end

                //退款 收入和明细 start  所有余额的消费账单 不在财务统计
                //退款 统计退款的房间的费用 而不是收入 以及 套餐的退款 房间的退款：如果是小时房 退款统计单价 如果是全日房 统计封顶价
                $timing1 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing2 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing0 = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $res['income']['tk_imcome'] = sprintf("%1\$.2f", $timing1) + sprintf("%1\$.2f", $timing2) + sprintf("%1\$.2f", $timing0);
                //订单统计（微信支付）
                $timing1_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing2_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing0_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type != 1")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $wxt1ids = "";
                if($timing1_ids){
                    foreach ($timing1_ids as $v){
                        $wxt1ids .= $v['id'].",";
                    }
                }
                if($wxt1ids != ""){
                    $wxt1ids = rtrim($wxt1ids,",");
                }
                $wxt2ids = "";
                if($timing2_ids){
                    foreach ($timing2_ids as $v){
                        $wxt2ids .= $v['id'].",";
                    }
                }
                if($wxt2ids != ""){
                    $wxt2ids = rtrim($wxt2ids,",");
                }
                $wxt0ids = "";
                if($timing0_ids){
                    foreach ($timing0_ids as $v){
                        $wxt0ids .= $v['id'].",";
                    }
                }
                if($wxt0ids != ""){
                    $wxt0ids = rtrim($wxt0ids,",");
                }


                $allckids = $wxt1ids."||".$wxt2ids."||".$wxt0ids;
                $res['list']['tk_list']   = $allckids;
                $res['list']['tk_sum']   = sprintf("%1\$.2f", $res['income']['tk_imcome']);
                //退款 收入和明细 end


                if($res){
                    $result = array("code"=>1,"rows" => $res);
                }else{
                    $result = array("code"=>0,"rows" => []);
                }

                return json($result);

            }else{

                //非超管 比如：商家管理员
                //订单收入  只统计 微信支付和余额支付 + 小时卡微信付款部分
                //微信支付+余额支付
                $order_all_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");
                //小时卡（微信部分付款的收入）
                $order_all_wx_income = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("wx_income");

                //退款产生的收益 也记录到现金 取消订单时 不论什么方式支付的，产生的收益记录到 income 而没有区分到 wx_income(退房记录到)
                $tk_income = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("income");

                //商家显示小时数
                $order_all_wx_hour = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("use_hour");
                //商家显示小时数 ids
                $order_hour_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $hourids = "";
                if($order_hour_ids){
                    foreach ($order_hour_ids as $v){
                        $hourids .= $v['id'].",";
                    }
                }
                if($hourids != ""){
                    $hourids = rtrim($hourids,",");
                }

                $res['list']['shop_all_ids'] = $hourids;

                //总收入
                $all_income = sprintf("%1\$.2f", $order_all_income) + sprintf("%1\$.2f", $order_all_wx_income) + sprintf("%1\$.2f", $tk_income);
                $res['income']['all_income'] = sprintf("%1\$.2f", $all_income);
                $res['income']['shop_all_hour'] = $order_all_wx_hour;
                $res['income']['shop_all_income'] = sprintf("%1\$.2f", $res['income']['all_income'] * 0.9);//普通商家 平台收取 10%费用

                //微信支付+余额支付 ids
                $wxlist = $oderdb->where("hotelid in ($hid)")->where("pay_type !=2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                //小时卡（微信部分付款的收入） ids
                $wxlist2 = $oderdb->where("hotelid in ($hid)")->where("pay_type = 2")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();

                $wxids = "";
                if($wxlist){
                    foreach ($wxlist as $v){
                        $wxids .= $v['id'].",";
                    }
                }
                if($wxids != ""){
                    $wxids = rtrim($wxids,",");
                }

                $wxids2 = "";
                if($wxlist2){
                    foreach ($wxlist2 as $v){
                        $wxids2 .= $v['id'].",";
                    }
                }
                if($wxids2 != ""){
                    $wxids2 = rtrim($wxids2,",");
                }

                //退款产生的收益 记录id
                $tk_income_ids = $oderdb->where("hotelid in ($hid)")->where("pay_type != 2")->where("income > 0 ")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $tkids = "";
                if($tk_income_ids){
                    foreach ($tk_income_ids as $v){
                        $tkids .= $v['id'].",";
                    }
                }
                if($tkids != ""){
                    $tkids = rtrim($tkids,",");
                }

                $allids = $wxids."||".$wxids2."||".$tkids;
                $res['list']['wx_list']   = $allids;

                //小时卡&微信 消费明细（订单）
                $xsklist = $oderdb->where("hotelid in ($hid)")->where("pay_type = '2'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->field("id")->select();
                $xsksum= $oderdb->where("hotelid in ($hid)")->where("pay_type = '2'")->where("pay_status = 3 and refund_status = 3 || pay_status = 3 and refund_status = 1 || pay_status = 3 and refund_status = 0")->sum("income");

                $xskids = "";
                if($xsklist){
                    foreach ($xsklist as $v){
                        $xskids .= $v['id'].",";
                    }
                }
                if($xskids != ""){
                    $xskids = rtrim($xskids,",");
                }
                $res['list']['xsk_list']   = $xskids;
                $res['list']['xsk_sum']   = sprintf("%1\$.2f", $xsksum);

                //退款 统计退款的房间的费用 而不是收入 以及 套餐的退款 房间的退款：如果是小时房 退款统计单价 如果是全日房 统计封顶价
                $timing1 = $oderdb->where("hotelid in ($hid)")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing2 = $oderdb->where("hotelid in ($hid)")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $timing0 = $oderdb->where("hotelid in ($hid)")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->sum("return_money");
                $res['income']['tk_imcome'] = sprintf("%1\$.2f", $timing1) + sprintf("%1\$.2f", $timing2) + sprintf("%1\$.2f", $timing0);

                //订单统计（微信支付）
                $timing1_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '1'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing2_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '2'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();
                $timing0_ids = $oderdb->where("hotelid in ($hid)")->where("timing = '0'")->where("pay_status = '1' and refund_status = '2' || pay_status = '1' and refund_status = '4' || pay_status = '4' and refund_status = '1' || pay_status = '4' and refund_status = '4' || pay_status = '2' and refund_status = '2' || pay_status = '4' and refund_status = '3'")->field("id")->select();

                $wxt1ids = "";
                if($timing1_ids){
                    foreach ($timing1_ids as $v){
                        $wxt1ids .= $v['id'].",";
                    }
                }
                if($wxt1ids != ""){
                    $wxt1ids = rtrim($wxt1ids,",");
                }

                $wxt2ids = "";
                if($timing2_ids){
                    foreach ($timing2_ids as $v){
                        $wxt2ids .= $v['id'].",";
                    }
                }
                if($wxt2ids != ""){
                    $wxt2ids = rtrim($wxt2ids,",");
                }

                $wxt0ids = "";
                if($timing0_ids){
                    foreach ($timing0_ids as $v){
                        $wxt0ids .= $v['id'].",";
                    }
                }
                if($wxt0ids != ""){
                    $wxt0ids = rtrim($wxt0ids,",");
                }

                $allckids = $wxt1ids."||".$wxt2ids."||".$wxt0ids;
                $res['list']['tk_list']   = $allckids;

                if($res){
                    $result = array("code"=>1,"rows" => $res);
                }else{
                    $result = array("code"=>0,"rows" => []);
                }

                return json($result);



            }


        }

    }



    public function ajaxIndexold(){
        //总条件：当前登陆身份
        $adminId = Session::get("admin")["id"];
        $hid = $this->request->request('hid');
        if($hid == 1){
            $hid = Session::get("admin")["hotelid"];
        }

        $starttime = $this->request->request('starttime');
        $endtime = $this->request->request('endtime');

        $todayS=strtotime($starttime); //int
        $todayE= strtotime($endtime);//int

        $oderdb = db("order");
        $oadb = db("cash");//订单申请提现表
        $res = [];
        $doingcashids = ""; //申请中
        $doingids = "";
        $docashids = "";//已处理
        $doids = "";
        $doingcash = $docash =  0;//申请中 收入金额


        if($adminId == 1){
            //小时卡业务统计
            //总余额 放到3期计算
        }

        if($todayS && !$todayE){
            //总收入【房费、基础服务费、退款费用】
            $idslist = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->field("id")->select();
            //总收入
            $income = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("income");
            //总金额
            $totalmoney = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("all_money");
            //总房费
            $allprice = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("all_price");
            //总基础服务费bprice
            $bprice = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("ballprice");
            //总押金
            // $deposit = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("deposit");

            //提现业务

            //申请中数据计算
            $alist = $oadb->where("hotelid in ($hid)")->where("status=1")->where("createtime >= $todayS")->field("id,oids")->select();
            if($alist){
                foreach ($alist as $v){
                    $doingcashids .= $v["oids"].",";
                    $doingids .= $v["id"].",";
                }
            }
            if($doingcashids != ""){
                $doingcashids  = rtrim($doingcashids,',');
                $doingids  = rtrim($doingids,',');
                $doingcash = $oderdb->where("id in ($doingcashids)")->where(["pay_status"=>3])->sum("income");
            }

            //已处理
            $alist2 = $oadb->where("hotelid in ($hid)")->where("status=2")->where("createtime >= $todayS")->field("id,oids")->select();
            if($alist2){
                foreach ($alist2 as $vv){
                    $docashids .= $vv["oids"].",";
                    $doids .= $vv["id"].",";

                }
            }
            if($docashids != ""){
                $docashids  = rtrim($docashids,',');
                $doids  = rtrim($doids,',');
                $docash = $oderdb->where("id in ($docashids)")->where(["pay_status"=>3])->sum("income");
            }

        }
        if(!$todayS && $todayE){
            $idslist = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->field("id")->select();
            $income = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->sum("income");
            $totalmoney = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->sum("all_money");
            $allprice = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->sum("all_price");
            $bprice = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->sum("ballprice");
            // $deposit = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayE")->where(["pay_status"=>3])->sum("deposit");
            //提现业务
            //申请中数据计算
            $alist = $oadb->where("hotelid in ($hid)")->where("status=1")->where("createtime <= $todayE")->field("id,oids")->select();
            if($alist){
                foreach ($alist as $v){
                    $doingcashids .= $v["oids"].",";
                    $doingids .= $v["id"].",";
                }
            }
            if($doingcashids != ""){
                $doingcashids  = rtrim($doingcashids,',');
                $doingids  = rtrim($doingids,',');
                $doingcash = $oderdb->where("id in ($doingcashids)")->where(["pay_status"=>3])->sum("income");
            }

            //已处理
            $alist2 = $oadb->where("hotelid in ($hid)")->where("status=2")->where("createtime <= $todayE")->field("id,oids")->select();
            if($alist2){
                foreach ($alist2 as $vv){
                    $docashids .= $vv["oids"].",";
                    $doids .= $vv["id"].",";

                }
            }
            if($docashids != ""){
                $docashids  = rtrim($docashids,',');
                $doids  = rtrim($doids,',');
                $docash = $oderdb->where("id in ($docashids)")->where(["pay_status"=>3])->sum("income");
            }

        }
        if($todayS && $todayE){
            $idslist = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->field("id")->select();
            $income = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("income");
            $totalmoney = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("all_money");
            $allprice = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("all_price");
            $bprice = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("ballprice");
            //  $deposit = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("deposit");
            //提现业务
            //申请中数据计算
            $alist = $oadb->where("hotelid in ($hid)")->where("status=1")->where("createtime>='$todayS' and createtime <= '$todayE'")->field("id,oids")->select();
            if($alist){
                foreach ($alist as $v){
                    $doingcashids .= $v["oids"].",";
                    $doingids .= $v["id"].",";
                }
            }
            if($doingcashids != ""){
                $doingcashids  = rtrim($doingcashids,',');
                $doingids  = rtrim($doingids,',');
                $doingcash = $oderdb->where("id in ($doingcashids)")->where(["pay_status"=>3])->sum("income");
            }

            //已处理
            $alist2 = $oadb->where("hotelid in ($hid)")->where("status=2")->where("createtime>='$todayS' and createtime <= '$todayE'")->field("id,oids")->select();
            if($alist2){
                foreach ($alist2 as $vv){
                    $docashids .= $vv["oids"].",";
                    $doids .= $vv["id"].",";
                }
            }
            if($docashids != ""){
                $docashids  = rtrim($docashids,',');
                $doids  = rtrim($doids,',');
                $docash = $oderdb->where("id in ($docashids)")->where(["pay_status"=>3])->sum("income");
            }


        }
        if(!$todayS && !$todayE){

            //总收入【房费、基础服务费、退款费用】
            $idslist = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->field("id")->select();
            $income = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("income");
            //总金额
            $totalmoney = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("all_money");
            $allprice = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("all_price");
            $bprice = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("ballprice");
            //  $deposit = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("deposit");

            //提现业务

            //申请中数据计算
            $alist = $oadb->where("hotelid in ($hid)")->where("status=1")->field("id,oids")->select();
            if($alist){
                foreach ($alist as $v){
                    $doingcashids .= $v["oids"].",";
                    $doingids .= $v["id"].",";
                }
            }
            if($doingcashids != ""){
                $doingcashids  = rtrim($doingcashids,',');
                $doingids  = rtrim($doingids,',');
                $doingcash = $oderdb->where("id in ($doingcashids)")->where(["pay_status"=>3])->sum("income");
            }

            //已处理
            $alist2 = $oadb->where("hotelid in ($hid)")->where("status=2")->field("id,oids")->select();
            if($alist2){
                foreach ($alist2 as $vv){
                    $docashids .= $vv["oids"].",";
                    $doids .= $vv["id"].",";
                }
            }
            if($docashids != ""){
                $docashids  = rtrim($docashids,',');
                $doids  = rtrim($doids,',');
                $docash = $oderdb->where("id in ($docashids)")->where(["pay_status"=>3])->sum("income");
            }


        }

        if($idslist){
            $res['income']['ids'] = arr2str($idslist);
        }else{
            $res['income']['ids'] = "";
        }

        //申请中
        $res['cash']['doingcashids'] = $doingcashids;
        $res['cash']['doingappid'] = $doingcashids;
        $res['cash']['doingcash'] = $doingcash;
        //已处理
        $res['cash']['docashids'] = $docashids;
        $res['cash']['docash'] = $docash;



        $res['income']['income'] = $income;//总收入
        $res['income']['allmoney'] = $totalmoney;//总金额
        $res['income']['allprice'] = $allprice;//总房费
        $res['income']['bprice'] = $bprice;//总基础服务费
        // $res['income']['deposit'] = $deposit;//总押金







        if($res){
            $result = array("code"=>1,"rows" => $res);
        }else{
            $result = array("code"=>0,"rows" => []);
        }

        return json($result);


    }


    //提现数据
    public function applist(){
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function index()
    {
        //总条件：当前登陆身份
        $adminId = Session::get("admin")["id"];
        $this->view->assign([
            'adminId' =>$adminId,
        ]);

        return $this->view->fetch();
    }

    public function indexold()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            foreach ($list as $k =>$v){
                $list[$k]['ctime']=date('Y-m-d H:i:s',$v['ctime']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
